<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: table_common_admincp_cmenu.php 27806 2012-02-15 03:20:46Z svn_project_zhangjie $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_home_video extends discuz_table
{
	public function __construct() {

		$this->_table = 'home_video';
		$this->_pk    = 'v_id';

		parent::__construct();
	}

	public function get_all_video($cate_id='', $start='', $pageSize='0'){
		if($pageSize != 0){
			if($cate_id == ''){
				return DB::fetch_all("SELECT * FROM %t vi LEFT JOIN %t vc ON vi.cate_id=vc.cate_id order by vi.v_sort limit %d,%d", array($this->_table, 'home_video_cate', $start, $pageSize));	
			}else{
				return DB::fetch_all("SELECT * FROM %t vi LEFT JOIN %t vc ON vi.cate_id=vc.cate_id WHERE vi.cate_id=%d order by vi.v_sort limit %d,%d", array($this->_table, 'home_video_cate', $cate_id, $start, $pageSize));
			}	
		}else{
			if($cate_id == ''){
				return DB::fetch_all("SELECT * FROM %t vi LEFT JOIN %t vc ON vi.cate_id=vc.cate_id order by vi.v_sort", array($this->_table, 'home_video_cate'));	
			}else{
				return DB::fetch_all("SELECT * FROM %t vi LEFT JOIN %t vc ON vi.cate_id=vc.cate_id WHERE vi.cate_id=%d order by vi.v_sort", array($this->_table, 'home_video_cate', $cate_id));
			}
		}
	}

	public function get_all_video_by_id($cate_id){
		return DB::fetch_all("SELECT * FROM %t WHERE cate_id=%d", array($this->_table, $cate_id));
	}

	public function add_video($data,$val){
		$col = array();
		$par = array();
		foreach ($data as $key => $value) {
			$col[] = $key;
			$par[] = $value;
		}
		$col_str = implode(',', $col);
		$val_str = implode(',', $val);
		$par_str = implode(',', $par);
		return DB::query('insert into %t ('.$col_str.') values ('.$val_str.')', array($this->_table, $data['v_image'], $data['v_name'], $data['cate_id'], $data['view_week'], $data['view_month'], $data['v_price'], $data['v_url'], $data['v_sort'], $data['add_time']));
	}

	public function del_video($v_id){
		return DB::query('delete from %t where v_id=%d',array($this->_table,$v_id));
	}

	public function get_video_by_id($v_id){
		return DB::fetch_first('select * from %t where v_id=%d',array($this->_table,$v_id));
	}

	public function update_video($data,$val,$id){
		$i = 0;
		$str = '';
		foreach ($data as $key => $value) {
			if(isset($val[$i])){
				if($i == 0){
					$str .= $key.'='.$val[$i];
				}else{
					$str .= ','.$key.'='.$val[$i];
				}
				$i++;
			}
		}

		return DB::query('update %t set '.$str.' where v_id=%d', array($this->_table,$data['v_image'], $data['v_name'], $data['cate_id'], $data['view_week'], $data['view_month'], $data['v_price'], $data['v_url'], $data['v_sort'], $id));

	}

	public function get_video_nums(){
		return DB::result_first('select count(v_id) from %t',array($this->_table));
	}







	public function get_show_pic(){
		return DB::fetch_all('select * from %t where is_show=1',array($this->_table));
	}

	public function get_pic_by_id($pid){
		return DB::fetch_first('select * from %t where pid=%d',array($this->_table,$pid));
	}

	public function update_cate($pid,$opt,$name,$sort){
		if(isset($opt)){
			return DB::query('delete from %t where cate_id=%d',array($this->_table,$pid));
		} else{
			if($name != '' && $sort != ''){
				return DB::query('update %t set cate_name=%s,cate_sort=%d where cate_id=%d', array($this->_table,$name,$sort,$pid));
			}elseif($name != '' && $sort == ''){
				return DB::query('update %t set cate_name=%s where cate_id=%d', array($this->_table,$name,$pid));
			}elseif($name == '' && $sort != ''){
				return DB::query('update %t set cate_sort=%d where cate_id=%d', array($this->_table,$sort,$pid));
			}
		}
	}

}

?>